{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "700c5360",
   "metadata": {},
   "source": [
    "## Fitting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "45dbd9c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fitting(A,b):\n",
    "    return np.linalg.inv(A.T@A)@(A.T@b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b95fd8ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calA(X):\n",
    "    #X=np.array(X)\n",
    "    A=np.zeros((X.size,2))\n",
    "    A[:,0]=1\n",
    "    A[:,1]=X\n",
    "    return A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7beccd1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calb(Y):\n",
    "    return Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "716b9dda",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bfb87f28",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "83dfa79f",
   "metadata": {},
   "outputs": [],
   "source": [
    "t=np.linspace(0,10,101)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2545f62b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ,\n",
       "        1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8,  1.9,  2. ,  2.1,\n",
       "        2.2,  2.3,  2.4,  2.5,  2.6,  2.7,  2.8,  2.9,  3. ,  3.1,  3.2,\n",
       "        3.3,  3.4,  3.5,  3.6,  3.7,  3.8,  3.9,  4. ,  4.1,  4.2,  4.3,\n",
       "        4.4,  4.5,  4.6,  4.7,  4.8,  4.9,  5. ,  5.1,  5.2,  5.3,  5.4,\n",
       "        5.5,  5.6,  5.7,  5.8,  5.9,  6. ,  6.1,  6.2,  6.3,  6.4,  6.5,\n",
       "        6.6,  6.7,  6.8,  6.9,  7. ,  7.1,  7.2,  7.3,  7.4,  7.5,  7.6,\n",
       "        7.7,  7.8,  7.9,  8. ,  8.1,  8.2,  8.3,  8.4,  8.5,  8.6,  8.7,\n",
       "        8.8,  8.9,  9. ,  9.1,  9.2,  9.3,  9.4,  9.5,  9.6,  9.7,  9.8,\n",
       "        9.9, 10. ])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f6eddc97",
   "metadata": {},
   "outputs": [],
   "source": [
    "R=1\n",
    "C=1\n",
    "i=5/R*np.exp(-t/(R*C))+np.random.rand(t.size)-0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "2483ebaf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5.11435397,  4.73157833,  4.45231002,  3.94855291,  3.39960759,\n",
       "        3.24784993,  2.78024939,  2.62101212,  2.1002981 ,  1.61438052,\n",
       "        1.92846998,  1.30651668,  1.25450646,  1.69309845,  1.52706101,\n",
       "        0.79006572,  1.41497403,  1.16702497,  1.07544037,  0.82016234,\n",
       "        0.77726247,  0.8436078 ,  0.60547164,  0.24318922,  0.00846824,\n",
       "        0.03094596,  0.48827836,  0.58514785,  0.41365686,  0.15795532,\n",
       "        0.14415029,  0.23990505,  0.50877952, -0.29449753, -0.27863319,\n",
       "       -0.3001608 ,  0.27130557, -0.08036699,  0.119112  , -0.00749584,\n",
       "        0.57729652,  0.10002458,  0.12244204, -0.24551687, -0.03298404,\n",
       "       -0.30912226, -0.05637496, -0.43881818,  0.38172014, -0.39164572,\n",
       "        0.01948485, -0.46661785,  0.37334614, -0.32666072,  0.26877905,\n",
       "       -0.24928002, -0.46257551, -0.28653336,  0.14749214,  0.46266225,\n",
       "        0.048923  ,  0.50589345,  0.21896255,  0.09377077,  0.03666096,\n",
       "        0.08156423, -0.11916538, -0.21551125,  0.06621341, -0.33722196,\n",
       "       -0.05411745, -0.43911314,  0.14725375,  0.00602846,  0.20424511,\n",
       "       -0.04161599,  0.11556247, -0.18707782,  0.22224712, -0.04477381,\n",
       "       -0.00649662, -0.21145049,  0.45354773,  0.04299006, -0.09216288,\n",
       "       -0.26697088, -0.27108418,  0.19159663, -0.16028835,  0.4393032 ,\n",
       "       -0.10533852, -0.08687067,  0.16738317, -0.05013013, -0.01118099,\n",
       "       -0.40180235, -0.05602911, -0.38459767,  0.08771051, -0.1805816 ,\n",
       "       -0.33529038])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "59e65597",
   "metadata": {},
   "outputs": [],
   "source": [
    "b=calb(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4d4efec2",
   "metadata": {},
   "outputs": [],
   "source": [
    "A=calA(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ac0ec8a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "result=fitting(A,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "da9b4ba4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.78761418, -0.25906403])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "aacf337f",
   "metadata": {},
   "outputs": [],
   "source": [
    "Rfitting=5/np.exp(result[0])\n",
    "Cfitting=-1/result[1]/Rfitting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "3963a5d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8367949118797067"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Rfitting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a4afc94",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
